package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.beauty.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.*;

public class BeautyDaoImpl extends TempDao {

    public Map<Integer, BeautyInfo> getBeautyInfoMap() {
        Map<Integer, BeautyInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_beauty";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                BeautyInfo data = new BeautyInfo();
                data.setBeautyId(rs.getInt("id"));
                data.setOccupation(rs.getInt("occupation"));
                data.setQuality(rs.getInt("quality"));
                data.setInitIntimacy(rs.getInt("intimacy"));
                data.setInitGlamour(rs.getInt("glamour"));
                data.setTotalProgress(rs.getInt("progress"));
                data.setQualification(rs.getInt("qualification"));
                data.setAddQualification(rs.getInt("addQualification"));
                data.setUpgradeParam(rs.getInt("upgradeParam"));
                data.setDefaultMaxLevel(rs.getInt("defaultMaxLevel"));
                data.setIdentitySkillId(rs.getInt("identitySkillId"));
                data.setIdentitySkillUpgradeMarkList(StringUtils.stringToIntegerList(rs.getString("identitySkillUpdate"), "\\|"));
                data.setIdentitySoundList(StringUtils.stringToIntegerList(rs.getString("identitySound"),"\\|"));
                data.setIcon(rs.getString("icon"));
                data.setSound(rs.getString("sound"));
                data.setAppearanceId(rs.getString("appearanceId"));
                String linkage = rs.getString("linkage");
                if ("0".equals(linkage)|| StringUtils.isNullOrEmpty(linkage)) {
                    data.setLinkAge(0);
                }else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
                    Date date = sdf.parse(linkage);
                    data.setLinkAge(date.getTime());
                }
                data.setHaloSkillList(StringUtils.stringToIntegerList(rs.getString("haloSkills"), ";"));
                data.setGenderSwapId(rs.getInt("genderSwapId"));
                data.setMusicRoomInitType(rs.getInt("musicRoomInitType"));
                dataMap.put(data.getBeautyId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, BeautyIdentityInfo> getBeautyIdentityInfoMap() {
        Map<Integer, BeautyIdentityInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_beauty_identity";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);

            rs = call.executeQuery();
            while (rs.next()) {
                BeautyIdentityInfo data = new BeautyIdentityInfo();
                data.setIdentityLv(rs.getInt("level"));
                data.setNeedIntimacy(rs.getInt("needIntimacy"));
                data.setNeedGlamour(rs.getInt("needGlamour"));
                data.setChildTalent(rs.getInt("childTalent"));
                data.setNeedBeautyCount(rs.getInt("needBeautyNum"));
                data.setApprenticeBonus(rs.getInt("apprenticeBonus"));
                String musicRoomSkill = rs.getString("musicRoomSkill");
                if (!StringUtils.isNullOrEmpty(musicRoomSkill) && !"0".equals(musicRoomSkill)){
                    List<BeautyMusicRoomSkillInfo> musicRoomSkillInfos = new ArrayList<>();
                    for (String s : musicRoomSkill.split("\\|")) {
                        musicRoomSkillInfos.add(new BeautyMusicRoomSkillInfo(s));
                    }
                    data.setMusicRoomSkill(musicRoomSkillInfos);
                }
                dataMap.put(data.getIdentityLv(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public Map<Integer, BeautySkillInfo> getBeautySkillInfoMap() {
        Map<Integer, BeautySkillInfo> dataMap = new HashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_beauty_skill";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                BeautySkillInfo data = new BeautySkillInfo();
                data.setSkillPos(rs.getInt("skillPos"));
                data.setNeedIntimacy(rs.getInt("needIntimacy"));
                data.setAddLevelLimit(rs.getInt("levelLimit"));
                data.setMinValue(rs.getInt("minValue"));
                data.setMaxValue(rs.getInt("maxValue"));
                data.setScholarAddition(rs.getBoolean("scholar"));
                data.setFarmerAddition(rs.getBoolean("farmer"));
                data.setCraftsmanAddition(rs.getBoolean("craftsman"));
                data.setBusinessmanAddition(rs.getBoolean("businessman"));
                data.setKnightAddition(rs.getBoolean("knight"));

                dataMap.put(data.getSkillPos(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public List<BeautyRelation> getBeautyRelationList() {
        List<BeautyRelation> dataList = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_p_beauty_relation";
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                BeautyRelation data = new BeautyRelation();
                data.setBeautyId(rs.getInt("beautyId"));
                data.setPatronsId(rs.getInt("patronsId"));
                data.setGetCondition(rs.getString("getCondition"));
                dataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return dataList;
    }
}
